package com.bite.order.config;

import com.bite.order.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderConfig {

    //配置RabbitTemplate
//    @Bean
//    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return rabbitTemplate;
//    }


    //配置RabbitMQ
    @Bean("orderQueue")
    public Queue orderQueue() {
        return QueueBuilder.durable(Constants.ORDER_DIRECT_QUEUE).build();
    }

    @Bean("extensionQueue1")
    public Queue extensionQueue1() {
        return QueueBuilder.durable(Constants.EXTENSION_DIRECT_QUEUE).build();
    }

    @Bean("orderExchange")
    public DirectExchange orderExchange() {
        return ExchangeBuilder.directExchange(Constants.ORDER_DIRECT_EXCHANGE).durable(true).build();
    }

    @Bean("orderBinding1")
    public Binding orderBinding1(@Qualifier("orderQueue") Queue queue, @Qualifier("orderExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(Constants.ORDER_DIRECT_ROUTING_KEY);
    }

    @Bean("orderBinding2")
    public Binding orderBinding2(@Qualifier("extensionQueue1") Queue queue, @Qualifier("orderExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ack");
    }

    @Bean("publisherConfirmQueue")
    public Queue publisherConfirmQueue() {
        return QueueBuilder.durable(Constants.PUBLISHER_CONFIRM_QUEUE).build();
    }

    @Bean("publisherConfirmExchange")
    public DirectExchange publisherConfirmExchange() {
        return ExchangeBuilder.directExchange(Constants.PUBLISHER_CONFIRM_EXCHANGE).durable(true).build();
    }

    @Bean("publisherConfirmBinding")
    public Binding publisherConfirmBinding(@Qualifier("publisherConfirmQueue") Queue queue, @Qualifier("publisherConfirmExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(Constants.PUBLISHER_CONFIRM_ROUTING_KEY);
    }

    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }

    @Bean("retryExchange")
    public DirectExchange retryExchange() {
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).durable(true).build();
    }

    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryQueue") Queue queue, @Qualifier("retryExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(Constants.RETRY_ROUTING_KEY).noargs();
    }
}
